/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-25 18:12:14
 * @LastEditors: TT
 * @LastEditTime: 2023-10-31 17:13:08
 */

import 'package:hzy_project_template/config/data_config/page_id_config.dart';
import 'package:hzy_project_template/config/third_package_config.dart';
import 'package:hzy_project_template/controllers/user_c.dart';
import 'package:hzy_project_template/models/models_index.dart';

import 'package:hzy_project_template/pages/login/network/login_api_utils.dart';
import 'package:hzy_project_template/utils/getx_util_tool.dart';
import 'package:hzy_project_template/utils/utils.dart';

class LoginVM {
  /// 配置登录
  configLogin({
    required String account,
    required String pwd,
    int type = 0,
    bool isLogin = true,
  }) async {
    var res = await LoginApiUtilS.loadLoginNetWork(
      account: account,
      pwd: pwd,
      isLogin: isLogin,
    );

    if (res.ok) {
      await UserC.current.updateUserInfo(
        model: UserModel.fromMap(res.data),
        isNeedConfigHeader: true,
      );
      if (!isLogin) {
        jumpToRootPage(
          context: Get.context!,
          name: PageIdConfig.index,
        );
      } else if (type == 0) {
        currentGoback();
      }
    } else {
      showToastMsg(res.error!.msg);
    }
  }

  /// 配置注册
  configRegister({
    required String account,
    required String nickName,
    required String pwd,
    ImageNormalModel? imageNormalModel,
  }) async {
    showNormalLoadMsg();
    var res = await LoginApiUtilS.loadRegisterNetWork(
      account: account,
      nickName: nickName,
      pwd: pwd,
      imageNormalModel: imageNormalModel,
    );
    loadDismiss();
    if (res.ok) {
      await UserC.current.updateUserInfo(
        model: UserModel.fromMap(res.data),
        isNeedConfigHeader: true,
      );
      jumpToRootPage(
        context: Get.context!,
        name: PageIdConfig.index,
      );
    } else {
      showToastMsg(res.error!.msg);
    }
  }
}
